//
//  BN_PingLunInput.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-4-1.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_PingLunInput.h"

@implementation BN_PingLunInput

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        

    }
    return self;
}

- (void)awakeFromNib {
    self.inputBox.layer.cornerRadius = 5.0;
    self.inputBox.delegate = self;
    [self performSelector:@selector(popupKeyboard) withObject:nil afterDelay:0.6 ];
    
    self.sendBtn.layer.cornerRadius = 5.0;
    [self.sendBtn setBackgroundImage:[UIImage imageNamed:@"fasong"] forState:UIControlStateNormal];
    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

// textView获得焦点，自动弹出键盘
- (void)popupKeyboard {
    // [self.inputBox becomeFirstResponder];
}

#pragma mark UIKeyboardWillShowNotification
// 当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    /**
     *  5s
     NSConcreteNotification 0x17005da30 {name = UIKeyboardDidShowNotification; userInfo = {
     UIKeyboardAnimationCurveUserInfoKey = 7;
     UIKeyboardAnimationDurationUserInfoKey = "0.25";
     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 184}}";
     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 660}";
     UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 476}";
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 184}}";
     UIKeyboardFrameChangedByUserInteraction = 0;
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 384}, {320, 184}}";
     }}
     
     4寸 模拟器
     NSConcreteNotification 0xa7999a0 {name = UIKeyboardDidShowNotification; userInfo = {
     UIKeyboardAnimationCurveUserInfoKey = 7;
     UIKeyboardAnimationDurationUserInfoKey = "0.25";
     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 676}";
     UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";
     UIKeyboardFrameChangedByUserInteraction = 0;
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
     }}
     */
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    NSString *height = [NSString stringWithFormat:@"%f",keyboardRect.size.height];
    [[NSNotificationCenter defaultCenter]postNotificationName:KPOPKEYBOARDHEIGHT object:height];
}

// 当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    // code...
}

// 当键加载完成时调用
- (void)keyboardDidShow:(NSNotification *)aNotification
{
    // code...
}

/**
 *  移除通知
 */
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    
}

#pragma mark
#pragma mark - UITextView Delegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if (textView.text.length == 0) {
        self.inputBox.placeholder = @"说点什么吧...";
        self.inputBox.textColor = [UIColor grayColor];
    }else{
        self.inputBox.text = @"";
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    // code...
}

@end
